package com.slt.developkitweb.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.google.common.base.Splitter;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/***
 * @author shalongteng
 * @date 2023年10月13日 8:45
 **/
public class LogUtil {
    public static String getStringLog(String lines, String log, String searchStr) {
        String[] split1 = log.split("\n");
        StringBuffer logbuffer = new StringBuffer();
        for (int i = split1.length - Integer.parseInt(lines); i < split1.length; i++) {
            String temp = split1[i].trim();
            //不传搜索字符串，添加全部日志
            if (StrUtil.isBlank(searchStr)) {
                logbuffer.append(temp);
                logbuffer.append("\n");
            } else {//
                if (temp.contains(searchStr)) {
                    logbuffer.append(temp);
                    logbuffer.append("\n");
                }
            }
        }
        return logbuffer.toString();
    }

    public static String getHtmlLog(String lines, String log, String searchStr) {
        List<String> list = Splitter.on("\n")
                .omitEmptyStrings()
                .splitToList(log);

        System.out.println(list.size());
        StringBuffer logbuffer = new StringBuffer();
        int lineNumber = 1;
        logbuffer.append("<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "<meta charset=\"utf-8\">\n" +
                "<title>生产环境数字人日志</title>\n" +
                "</head>\n" +
                "<body>\n" + "<h1 align=\"center\">生产环境数字人日志</h1>");
        for (int i = list.size() - Integer.parseInt(lines); i < list.size(); i++) {
            String temp = list.get(i).trim();
            if(StrUtil.isBlank(temp))continue;
            //不传搜索字符串，添加全部日志
            if (StrUtil.isBlank(searchStr)) {
                logbuffer.append("<p>");
                logbuffer.append(lineNumber++ + " : " + list.get(i).trim());
                logbuffer.append("</p>");
                logbuffer.append("\n");
            } else {
                if (temp.contains(searchStr)) {
                    logbuffer.append("<p>");
                    logbuffer.append(lineNumber++ + " : " + list.get(i).trim());
                    logbuffer.append("</p>");
                    logbuffer.append("\n");
                }
            }
        }
        logbuffer.append("</body>\n" + "</html>");
        return logbuffer.toString();
    }
}
